package edu.guet.server.controller;

import edu.guet.server.model.dto.UpdateUserInfoDTO;
import edu.guet.server.model.vo.UserInfoVO;
import edu.guet.server.service.UserInfoService;
import edu.guet.server.utils.ResultAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping
@Slf4j
public class UserInfoController {

    @Autowired
    private UserInfoService infoService;

    /**
     * 根据id获取用户信息
     * @param user_id
     * @return
     */
    @GetMapping("/student/showinfo")
    public ResultAPI showUserInfo(@RequestParam int user_id){
        UserInfoVO userInfoVO = infoService.getUserInfo(user_id);
        if(userInfoVO == null){
            return ResultAPI.error("未找到该用户或该用户不是学生");
        }
        return ResultAPI.success(userInfoVO);
    }

    /**
     *  修改用户基本信息
     * @param userInfoDTO
     * @return
     */
    @PutMapping("/student/updateinfo")
    public ResultAPI userUpdateInfo(@RequestBody UpdateUserInfoDTO userInfoDTO){
        infoService.updateUserInfo(userInfoDTO);
        UserInfoVO userInfoVO = infoService.getUserInfo(userInfoDTO.getUser_id());
        log.info("传入的用户id为："+String.valueOf(userInfoDTO.getUser_id()));
        if(userInfoVO == null){
            return ResultAPI.error("更新失败");
        }
        return ResultAPI.success(userInfoVO);
    }

    /**
     * 修改头像
     * @param file
     * @param user_id
     * @return
     */
    @PostMapping("/student/updateAvatar")
    public ResultAPI updateAvatar(@RequestParam MultipartFile file, @RequestParam int user_id){

        return infoService.updateAvatar(file, user_id);
    }
}
